package com.topscore.uke.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.topscore.uke.R;
import com.topscore.utillibrary.widget.wheelview.ArrayWheelAdapter;
import com.topscore.utillibrary.widget.wheelview.WheelView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 71468 on 2017/11/29.
 */

public class MonthSelector extends Dialog implements View.OnClickListener {
    @Bind(R.id.cancel_btn)
    TextView cancelBtn;
    @Bind(R.id.confirm_btn)
    TextView confirmBtn;
    @Bind(R.id.year_wheel)
    WheelView yearWheel;
    @Bind(R.id.month_wheel)
    WheelView monthWheel;

    private Context mContext;
    private OnMonthSelectedListener mSelectedListener;

    private List<String> mYearArray, mMonthArray;
    private HashMap<String, List<String>> mYearMonthMap;

    public MonthSelector(@NonNull Context context, OnMonthSelectedListener onMonthSelectedListener) {
        super(context);
        this.mContext = context;
        this.mSelectedListener = onMonthSelectedListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();

        window.setWindowAnimations(R.style.DialogBottomAnimation);
        window.setGravity(Gravity.BOTTOM);
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.month_selector_layout);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        initData();
        yearWheel.setCyclic(false);
        monthWheel.setCyclic(false);
        yearWheel.setAdapter(new ArrayWheelAdapter((ArrayList<String>) mYearArray));
        yearWheel.setCurrentItem(0);
        mMonthArray = mYearMonthMap.get(mYearArray.get(0));
        monthWheel.setAdapter(new ArrayWheelAdapter((ArrayList) mMonthArray));
        yearWheel.setOnItemSelectedListener(index -> {
            mMonthArray = mYearMonthMap.get(mYearArray.get(index));
            monthWheel.setAdapter(new ArrayWheelAdapter((ArrayList) mMonthArray));
            monthWheel.setCurrentItem(0);
        });
        cancelBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
    }

    private void initData() {
        mYearArray = new ArrayList<>();
        mYearMonthMap = new HashMap<>();
        mYearArray.add("本年");
        for (int i = 0; i < 10; i++) {
            Date date = new Date();
            Calendar cal = Calendar.getInstance();
            cal.clear();
            cal.setTime(date);
            cal.add(Calendar.YEAR, -i);
            String year = new SimpleDateFormat("YYYY").format(cal.getTime());
            mYearArray.add(year);
        }
        List<String> monthArray;
        for (int i = 0; i < mYearArray.size(); i++) {
            if (!mYearArray.get(i).equals("本年")) {
                if (mYearArray.get(i).equals(new SimpleDateFormat("YYYY").format(new Date()))) {
                    monthArray = new ArrayList<>();
                    monthArray.add("全年");
                    int month = Calendar.getInstance().get(Calendar.MONTH);
                    for (int j = 0; j < month + 1; j++) {
                        Calendar cal = Calendar.getInstance();
                        cal.clear();
                        cal.set(Calendar.MONTH, j);
                        monthArray.add(new SimpleDateFormat("MM").format(cal.getTime()));
                    }
                } else {
                    int month = 12;
                    monthArray = new ArrayList<>();
                    monthArray.add("全年");
                    for (int j = 0; j < month; j++) {
                        Calendar cal = Calendar.getInstance();
                        cal.clear();
                        cal.set(Calendar.MONTH, j);
                        monthArray.add(new SimpleDateFormat("MM").format(cal.getTime()));
                    }
                }
            } else {
                monthArray = new ArrayList<>();
                monthArray.add("全年");
                monthArray.add("本月");
            }
            mYearMonthMap.put(mYearArray.get(i), monthArray);
        }

    }

    @Override
    public void show() {
        super.show();
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel_btn:
                dismiss();
                break;
            case R.id.confirm_btn:
                onConfirm();
                break;

        }
    }

    public void onConfirm() {
        String year = mYearArray.get(yearWheel.getCurrentItem());
        String month = mMonthArray.get(monthWheel.getCurrentItem());
        if (month.equals("本月")) {
            mSelectedListener.onSelectCurrentMonth();
        } else if (month.equals("全年") && year.equals("本年")) {
            mSelectedListener.onSelectCurrentYear();
        } else if (month.equals("全年")) {
            mSelectedListener.onSelectYear(year);
        } else {
            String date = year + "-" + month;
            mSelectedListener.onSelectMonth(year,month);
        }

        dismiss();
    }

    public interface OnMonthSelectedListener {
        void onSelectYear(String year);

        void onSelectMonth(String year,String month);

        void onSelectCurrentYear();

        void onSelectCurrentMonth();
    }
}
